package cn.edu.suda.android.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.suda.android.bean.VoteInfo;
import cn.edu.suda.android.bean.Voter;
import cn.edu.suda.android.service.VoteInfoService;
import cn.edu.suda.android.service.VoterService;

@Controller
public class VoteController {

	@Autowired
	private VoterService voterService;
	@Autowired
	private VoteInfoService voteInfoService;

	@RequestMapping("/setVoteInfo")
	@ResponseBody
	public int setVoteInfo(String model) {
		// destroy
		int i = voteInfoService.destroyVoteInfo(model);

		List<Voter> voters = voterService.selectVoter();
		List<VoteInfo> voteInfos = new ArrayList<>();
		for (Voter voter : voters) {
			VoteInfo voteInfo = new VoteInfo();
			voteInfo.setViModel(model);
			voteInfo.setViToVoter(voter.getvId());
			voteInfos.add(voteInfo);
		}
		int j = voteInfoService.insertVoteInfo(voteInfos);

		return (i > 0 && j > 0) ? 1 : 0;
	}

	@RequestMapping("/getVoteInfo")
	@ResponseBody
	public List<VoteInfo> getVoteInfo(String model) {
		return voteInfoService.getVoteInfo(model);
	}

	@RequestMapping("/updateVoteInfo")
	@ResponseBody
	public int updateVoteInfo(VoteInfo voteInfo) {
		return voteInfoService.updateVoteInfo(voteInfo);
	}
	
	@RequestMapping("/getResult")
	@ResponseBody
	public List<VoteInfo> getResult() {
		return voteInfoService.getVoteResult();
	}

}
